<?php

require_once( '../../libs/smarty.inc' );

require_once( '../../libs/smt_variables.php' );

require_once( '../../libs/Class_DB.php' );

require_once( '../../libs/Class_ERROR.php' );

require_once( '../../libs/login.inc.php' );

require_once( '../../libs/const.php' );



define('PAGE_MAX', 30 );





if( !in_array( "A", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;



if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){

	$page['current'] = $_GET['page'];

	$st = ($page['current'] -1) * PAGE_MAX;

}else{

	$page['current'] = 1;

	$st = 0;

}



if( isset( $_GET['category'] ) ){

	$select_category = $_GET['category'];

	if( $select_category ){

		$cnt_sql	= sprintf("SELECT COUNT(*) AS cnt FROM M_PRODUCTS WHERE category = %u", $select_category);

		$sql		= sprintf("SELECT * FROM M_PRODUCTS WHERE category = %u ORDER BY category, pro_name LIMIT %u, %u", $select_category, $st, PAGE_MAX);

	}else{

		$cnt_sql	= "SELECT COUNT(*) AS cnt FROM M_PRODUCTS";

		$sql		= sprintf("SELECT * FROM M_PRODUCTS ORDER BY category, pro_name LIMIT %u, %u", $st, PAGE_MAX );

	}

	

}else{

	$select_category = 0;

	$cnt_sql	= "SELECT COUNT(*) AS cnt FROM M_PRODUCTS";

	$sql		= sprintf("SELECT * FROM M_PRODUCTS ORDER BY category, pro_name LIMIT %u, %u", $st, PAGE_MAX );

}



$db = new Class_DB;



$db->query( $cnt_sql );

$row = $db->fetch();

$page['cnt'] = $row['cnt'];

$page['end'] = floor( $page['cnt'] / PAGE_MAX );

if( $page['cnt'] % PAGE_MAX ) $page['end']++;



for( $i = 1; $i <= $page['end']; $i++ ){

	$page['paging'][] = $i;

}



$page['item'] = PAGE_MAX;





$db->query( $sql );

while( $row = $db->fetch() ){

	$data[] = $row;

}



$category[5] = '製品';

array_unshift($category, '');

$smarty->assign('category', $category);

$smarty->assign('select_category', $select_category);

if( isset($data) ) $smarty->assign('data', $data);

$smarty->assign('page', $page);

$smarty->display('web-admin/products_index.html');



?>